#include "testrgb_qt.h"
#include "ui_testrgb_qt.h"
#include  <QPainter>
#include  <QImage>
#include <SDL2/SDL.h>
#include <QDebug>
#include <qmessagebox.h>
#include <QImageReader>
#include <QFile>
#include <fstream>
#include <QFileDialog>
#include <iostream>
#include "xvideoview.h"
#include  <vector>
#include <sstream>
using namespace std;

static std::vector<XVideoView*> views;

static SDL_Window* sdl_win = nullptr;
static SDL_Renderer* sdl_render = nullptr;
static SDL_Texture* sdl_texture = nullptr;
static unsigned char* yuv = nullptr;
static int sdl_width = 0;
static int sdl_height = 0;
static int pixel_size = 2;
static ifstream yuv_file;
static XVideoView* view = nullptr;

void TestRGB_QT::timerEvent(QTimerEvent* ev)
{
    yuv_file.read((char*)yuv, sdl_height * sdl_width * 1.5);
     size_t bytes_read = yuv_file.gcount();
     if(bytes_read < sdl_height * sdl_width * 1.5)
     {
         yuv_file.clear();
         yuv_file.seekg(0, std::ios::beg);
     }
    view->mirrorYUV420_Horizontal(yuv, sdl_width, sdl_height);
    view->Draw(yuv);
    if(view->IsExit())
    {
        view->Close();
        exit(0);
    }
}

void TestRGB_QT::View()
{
    static int fps_arr[32] = {0};
    fps_arr[0] = ui->set_fps1->value();
    fps_arr[1] = ui->set_fps1_2->value();
    static long long last_render_time[32] = {0};

    for(int i = 0; i < views.size(); i++)
    {
        if(fps_arr[i] <= 0)continue;
        int interval_time = 1000 / fps_arr[i];
        if((NOwMs() - last_render_time[i] < interval_time))
        {
            continue;
        }
        else
        {
            last_render_time[i] = NOwMs();
        }
        std::cout << NOwMs() << "===" << last_render_time[i]<< std::endl;


        auto frame = views[i]->Read();
        if(!frame){
            continue;
        }
        views[i]->DrawFrame(frame);
        //显示fps
        stringstream ss;
        ss << "fps:" << views[i]->Getrender_fps();
        if(i == 0)
        {
            ui->fps1->setText(ss.str().c_str());
        }
        else
        {
            ui->fps2->setText(ss.str().c_str());
        }
    }

}

void TestRGB_QT::Main()
{
    while(!is_exit_)
    {
        ViewS();
        MSleep(1);
    }
}

TestRGB_QT::TestRGB_QT(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestRGB_QT)
{
    ui->setupUi(this);
    connect(ui->open1, &QPushButton::clicked, this, &TestRGB_QT::Open1);
    connect(ui->open1_2, &QPushButton::clicked, this, &TestRGB_QT::Open2);
    connect(this, SIGNAL(ViewS()), this, SLOT(View()));
    views.push_back(XVideoView::Create());
    views.push_back(XVideoView::Create());
    views[0]->set_win_id((void*)ui->video1->winId());
    views[1]->set_win_id((void*)ui->video2->winId());
    th_ = std::thread(&TestRGB_QT::Main, this);
}

TestRGB_QT::~TestRGB_QT()
{
    delete ui;
}

void TestRGB_QT::resizeEvent(QResizeEvent *event)
{
//    ui->label->resize(size());
//    ui->label->move(0, 0);
//    view->Scale(width(), height());
}
void TestRGB_QT::Open1()
{
    Open(0);
}

void TestRGB_QT::Open2()
{
    Open(1);

}

void TestRGB_QT::Open(int i)
{
    QFileDialog fd;
    auto filename = fd.getOpenFileName();
    if(filename.isEmpty())return;
    cout << filename.toLocal8Bit().data() << endl;
    //打开文件
    if(!views[i]->Open(filename.toStdString()))
    {
        return;
    }
    //初始化窗口
    int w = 0;
    int h = 0;
    QString pix;
    if(i == 0)
    {
        w = ui->width1->value();
        h = ui->height1->value();
        pix = ui->pix1->currentText();

    }
    else
    {
        w = ui->width1_2->value();
        h = ui->height1_2->value();
        pix = ui->pix1_2->currentText();
    }
    XVideoView::Format fmt = XVideoView::YUV420P;
    if(pix == "YUV420")
    {
        fmt = XVideoView::YUV420P;
    }
    else if(pix == "RGBA")
    {
         fmt = XVideoView::RGBA;
    }
    else if(pix == "ARGB")
    {
         fmt = XVideoView::ARGB;
    }
    else if(pix == "BGRA")
    {
         fmt = XVideoView::BGRA;
    }
    std::cout << "file format:" <<fmt << std::endl;
    views[i]->InIt(w, h, fmt);

}

